package hittegods.gui;

import hittegods.classes.Item;
import hittegods.classes.Item.Category;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import javax.swing.*;

public class addElementPnl extends JPanel {

    private JLabel caseNoLbl;
    private JTextField caseNoFld;
    private JLabel descLbl;
    private JTextArea descTxt;
    private JTextField handinFld;
    private JLabel handinLbl;
    private JTextField foundDFld;
    private JLabel foundDLbl;
    private JTextField foundPFld;
    private JLabel foundPLbl;
    private JTextField finderFld;
    private JLabel finderLbl;
    private JTextField ownerFld;
    private JLabel ownerLbl;
    private JTextField rewardFld;
    private JLabel rewardLbl;
    private JComboBox categoryBox;
    private JButton saveBtn;
    private Mainscreennew parent;

    public addElementPnl() {
        setLayout(null);
        init();
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tilføj ny effekt", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 18)));
        setBounds(0, 0, WIDTH - 25, HEIGHT - 25);

        add(caseNoLbl);
        Dimension size = caseNoLbl.getPreferredSize();
        caseNoLbl.setBounds(20, 25, size.width, 23);

        add(caseNoFld);
        size = caseNoFld.getPreferredSize();
        caseNoFld.setBounds(20, 50, 150, 23);

        add(handinLbl);
        size = handinLbl.getPreferredSize();
        handinLbl.setBounds(220, 25, size.width, 23);

        add(handinFld);
        size = handinFld.getPreferredSize();
        handinFld.setBounds(220, 50, 150, 23);

        add(foundDLbl);
        size = foundDLbl.getPreferredSize();
        foundDLbl.setBounds(420, 25, size.width, 23);

        add(foundDFld);
        size = foundDFld.getPreferredSize();
        foundDFld.setBounds(420, 50, 150, 23);

        add(foundPLbl);
        size = foundPLbl.getPreferredSize();
        foundPLbl.setBounds(20, 85, size.width, 23);

        add(foundPFld);
        size = foundPFld.getPreferredSize();
        foundPFld.setBounds(20, 110, 250, 23);

        add(finderLbl);
        size = finderLbl.getPreferredSize();
        finderLbl.setBounds(20, 135, size.width, 23);

        add(finderFld);
        size = finderFld.getPreferredSize();
        finderFld.setBounds(20, 160, 250, 23);

        add(ownerLbl);
        size = ownerLbl.getPreferredSize();
        ownerLbl.setBounds(20, 185, size.width, 23);

        add(ownerFld);
        size = ownerFld.getPreferredSize();
        ownerFld.setBounds(20, 210, 250, 23);

        add(rewardLbl);
        size = rewardLbl.getPreferredSize();
        rewardLbl.setBounds(20, 235, size.width, 23);

        add(rewardFld);
        size = rewardFld.getPreferredSize();
        rewardFld.setBounds(20, 260, 250, 23);

        add(categoryBox);
        size = categoryBox.getPreferredSize();
        categoryBox.setBounds(300, 100, 270, size.height);

        add(descLbl);
        size = descLbl.getPreferredSize();
        descLbl.setBounds(300, 135, size.width, 23);

        add(descTxt);
        size = descTxt.getPreferredSize();
        descTxt.setBounds(300, 160, 270, 120);

        add(saveBtn);
        size = saveBtn.getPreferredSize();
        saveBtn.setBounds(20, 295, size.width, size.height);
        saveBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Item.insertItem(caseNoFld.getText(), ((Item.Category) categoryBox.getSelectedItem()).getCatId(), descTxt.getText(), fixDate(foundDFld.getText()), foundPFld.getText(), finderFld.getText(), ownerFld.getText(), rewardFld.getText());
            }
        });
    }

    private Date fixDate(String date) {
        return Date.valueOf(date.substring(6, 10) + "-" + date.substring(3, 5) + "-" + date.substring(0, 2));
    }
    private void init() {
        caseNoLbl = new JLabel("Sags nummer");
        caseNoFld = new JTextField();
        handinLbl = new JLabel("Indleveringsdato");
        handinFld = new JTextField("DD-MM-ÅÅÅÅ");
        foundDLbl = new JLabel("Fundet dato");
        foundDFld = new JTextField("DD-MM-ÅÅÅÅ");
        foundPLbl = new JLabel("Findested");
        foundPFld = new JTextField();
        finderLbl = new JLabel("Finders navn");
        finderFld = new JTextField();
        ownerLbl = new JLabel("Ejers navn");
        rewardLbl = new JLabel("Ønsket findeløn");
        rewardFld = new JTextField();
        ownerFld = new JTextField();
        descLbl = new JLabel("Generel beskrivelse");
        descTxt = new JTextArea();
        categoryBox = Item.createCatComboBox();
        saveBtn = new JButton("Gem");
    }
}
